package UIUtils
{
	import UIFree.uif_loader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.IBitmapDrawable;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class uif_utilBitmap {
		public static function loadTransImage(filePath: String, fileMaskPath: String, _jpgBitmap: Bitmap, onComplete: Function): void {
			var l_tmpl		: uif_loader;
			var l_mask		: uif_loader;
			
			function l_doLoad(e: Event): void {
				if (l_tmpl.loaded && l_mask.loaded) {
					var size: Point = new Point(l_tmpl.contentLoaderInfo.width, l_tmpl.contentLoaderInfo.height);
					
					_jpgBitmap.bitmapData = new BitmapData(size.x, size.y, true, 0x0);
					_jpgBitmap.bitmapData.draw(l_tmpl);
					
					var bmdMask: BitmapData = new BitmapData(size.x, size.y, true, 0xFFFFFF);
					bmdMask.draw(l_mask);
					_jpgBitmap.bitmapData.copyChannel(bmdMask, bmdMask.rect, new Point(0, 0), BitmapDataChannel.BLUE, BitmapDataChannel.ALPHA);
					onComplete(_jpgBitmap);
					bmdMask.dispose();
					l_tmpl.unload();
					l_mask.unload();
				}
			}
			
			l_tmpl = new uif_loader(null, filePath, l_doLoad);
			l_mask = new uif_loader(null, fileMaskPath, l_doLoad);
		}
		
		public static function calcAlphaRect(templateImage: DisplayObject, inflateRect:Number=1/20): Rectangle {
			var bitmapData: BitmapData = new BitmapData(templateImage.width, templateImage.height, false, 0);
			bitmapData.draw(templateImage, null, new ColorTransform(1, 1, 1, 3, 0, 0, 0, -240), BlendMode.ERASE);
			
			var alphaRect: Rectangle = bitmapData.getColorBoundsRect(0xFFFFFFFF, 0, true);
			alphaRect.inflate(alphaRect.width * inflateRect, alphaRect.height * inflateRect);
			bitmapData.dispose();
			alphaRect.width = Math.floor(alphaRect.width);
			alphaRect.height = Math.floor(alphaRect.height);
			
			return alphaRect;
		}
		
		public static function calcAlphaRectMulti(maskInfo: Object, bmdMask: BitmapData, inflateRect:Number=1/20): Array {
			var i:int;
			var _photos: Array = new Array();
			var lrect: Rectangle;
			var tmpBmp: BitmapData = bmdMask.clone();
			var np	: Point 	= new Point(0, 0);
			tmpBmp.threshold(tmpBmp, tmpBmp.rect, np, '<', 0xEE000000, 0xFFFFFFFF, 0xFF000000, false);
			
			for (i=0; i<maskInfo.spots.length; i++) {
				var sp2: Array = maskInfo.spots[i];
				_photos[i] = new Object();
				if (sp2.length == 2) {
					var color: uint = 0xFFFF0000 | (Math.round(i/maskInfo.spots.length) * 0xFF);
					tmpBmp.floodFill(sp2[0], sp2[1], color);
					lrect = tmpBmp.getColorBoundsRect(0xFFFFFFFF, color, true);
					
					lrect.inflate(lrect.width * inflateRect, lrect.height * inflateRect);
					if (maskInfo.ismask) {
						var sh: Bitmap = new Bitmap(new BitmapData(lrect.width, lrect.height, true, 0xFFFFFFFF));
						sh.bitmapData.threshold(tmpBmp, lrect, np, '==', color, 0, 0xFFFFFFFF, false);
						sh.bitmapData.applyFilter(sh.bitmapData, sh.bitmapData.rect, np, new BlurFilter(2, 2, 3));
						/*sh.bitmapData.threshold(sh.bitmapData, sh.bitmapData.rect, np, '>', 0xBB000000, 0xFFFFFFFF, 0xFF000000, false);*/
						_photos[i].mask = sh;
					}
				} else lrect = new Rectangle(sp2[0], sp2[1], sp2[2] - sp2[0], sp2[3] - sp2[1]);
				_photos[i].rect = lrect;
			}
			tmpBmp.dispose();
			return _photos;
		}
		
		public static function loadAsBitmap(url: String, doComplete: Function, asTransparent: Boolean = true, fillColor: uint=0): Bitmap {
			var bmp: Bitmap = new Bitmap();
			var _loader: uif_loader = new uif_loader(null, url, function(e: Event): void {
				bmp.bitmapData = new BitmapData(_loader.contentLoaderInfo.width, _loader.contentLoaderInfo.height, asTransparent, fillColor);
				bmp.bitmapData.draw(_loader);
				doComplete(bmp);
			});
			return bmp;
		}
		
		public static function copyBitmapData(source: DisplayObject, scale: Number=1): BitmapData {
			var _size: Point = new Point(source.width * scale, source.height * scale);
			var _clone: BitmapData = new BitmapData(_size.x, _size.y, true, 0);
			_clone.draw(source, new Matrix(scale, 0, 0, scale), null, null, null, true);
			
			return _clone;
		}
		
		public static function copyByteArray(source: ByteArray, doComplete: Function): void {
			var loader: Loader = new Loader();
			function doLComplete(e: Event): void {
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, doLComplete);
				var bmd: BitmapData = new BitmapData(loader.contentLoaderInfo.width, loader.contentLoaderInfo.height, true, 0);
				bmd.draw(loader);
				doComplete(bmd);
			}
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, doLComplete);
			loader.loadBytes(source);
		}
/*		
		public static function loadAlternative(link: String, a_loader: Loader): void {
			var loader: URLLoader = new URLLoader(new URLRequest(link));
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, doComplete);
			
			function doComplete(e: Event): void {
				a_loader.loadBytes(e.target.data as ByteArray);
			}			
		}
*/		
	}
}